package com.lib.common.repository.user

import android.annotation.SuppressLint
import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.lib.common.api.ApiService
import com.lib.common.entity.api.resp.UserInfo
import com.lib.common.helper.api.ApiHelper
import com.lib.common.helper.api.ApiResponse
import com.lib.common.helper.api.ComRequestCallback
import com.lib.framework.manager.ContextManager
import com.lib.framework.manager.thread.bgThreadRun

/**
 * Copyright (C), 2020-2020, 享物说
 * FileName: UserInfoRepository
 * Author: keanbin
 * Date: 2020-02-07 16:01
 * Description: 用户信息仓库
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class UserInfoRepository private constructor(context: Context) {

    companion object {

        @Volatile
        @SuppressLint("StaticFieldLeak")
        private var INSTANCE: UserInfoRepository? = null

        @JvmStatic
        private fun getInstance(context: Context): UserInfoRepository {
            if (INSTANCE == null) {
                synchronized(UserInfoRepository::class) {
                    if (INSTANCE == null) {
                        INSTANCE = UserInfoRepository(context)
                    }
                }
            }
            return INSTANCE!!
        }

        @JvmStatic
        fun getInstance(): UserInfoRepository {
            return getInstance(ContextManager.applicationContext)
        }
    }

    /**
     * 用户信息 sp存储 辅助类
     */
    private val mUserInfoSharePreHelper = UserInfoSharePreHelper(context)

    /**
     * 用户信息
     */
    private var mUserInfo = mUserInfoSharePreHelper.getUserInfo()

    /**
     * 用户信息 LiveData
     */
    val mUserInfoLiveData = MutableLiveData<UserInfo>()

    init {
        mUserInfoLiveData.postValue(mUserInfo)
    }

    /**
     * 从服务器获取用户信息
     */
    fun getUserInfoFromServer() {
        ApiHelper.getService(ApiService::class.java)?.userinfo()
            ?.request(object : ComRequestCallback<ApiResponse<UserInfo>>() {
                override fun onSuccess02(data: ApiResponse<UserInfo>?) {
                    if (data?.success != true) {
                        return
                    }

                    bgThreadRun {
                        data.data?.let { userInfo ->
                            saveUserInfo(userInfo)
                        }
                    }
                }

                override fun onFailure(t: Throwable) {

                }
            })
    }

    /**
     * 获取 用户信息
     */
    fun getUserInfo(): UserInfo? {
        return mUserInfo
    }

    /**
     * 获取用户ID
     */
    fun getUserId(): String {
        return mUserInfo?.userId ?: ""
    }

//    /**
//     * 保存 用户信息
//     */
//    fun saveUserInfo(setUserInfoReq: SetUserInfoReq) {
//        val userInfo = mUserInfo ?: return
//
//        var isChange = false
//        setUserInfoReq.avatar?.let {
//            userInfo.avatar = it
//            isChange = true
//        }
//
//        setUserInfoReq.nickName?.let {
//            userInfo.nickName = it
//            isChange = true
//        }
//
//        setUserInfoReq.birthDay?.let {
//            userInfo.birthday = it
//            isChange = true
//        }
//
//        setUserInfoReq.gender?.let {
//            userInfo.gender = it
//            isChange = true
//        }
//
//        setUserInfoReq.selfDesc?.let {
//            userInfo.selfDesc = it
//            isChange = true
//        }
//
//        setUserInfoReq.covers?.let {
//            userInfo.covers = it
//            isChange = true
//        }
//
//        setUserInfoReq.cityText?.let {
//            userInfo.areaHome = it
//            isChange = true
//        }
//
//        if (isChange) {
//            saveUserInfo(userInfo)
//        }
//    }

    /**
     * 保存 用户信息
     */
    fun saveUserInfo(userInfo: UserInfo) {
        mUserInfo = userInfo
        mUserInfoSharePreHelper.saveUserInfo(userInfo)
        mUserInfoLiveData.postValue(mUserInfo)
    }

    /**
     * 清除
     */
    fun clear() {
        mUserInfoLiveData.postValue(null)
        mUserInfo = null
        mUserInfoSharePreHelper.clearUserInfo()
    }
}